class CombinationIterator(object):

    def __init__(self, characters, combinationLength):
        """
        :type characters: str
        :type combinationLength: int
        """
        self.c = characters
        self.n = len(characters)
        self.k = combinationLength
        self.val = 0
        self.min_val = 0
        for i in range(self.k):
            self.min_val = self.min_val | (1 << i)
            self.val = self.val | (1 << (self.n - i - 1))

    def next(self):
        """
        :rtype: str
        """
        while self.val.bit_count() != self.k:
            self.val -= 1
        t = ''
        for i in range(self.n - 1, -1, -1):
            if (self.val >> i) & 1 == 1:
                t += self.c[self.n - 1 - i]
        self.val -= 1
        return t


    def hasNext(self):
        """
        :rtype: bool
        """
        return self.val >= self.min_val
